using System;
using System.IO;
using System.Data;
using System.Xml;
using System.Xml.Serialization;

using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using Policy = PHSRAG.Insight21.Policy;
using PHSRAG.Insight21.InsightUtilities;
using CDS=PHSRAG.Insight21.CDS;
using PHSRAG.Utility;

namespace PHSRAG.Insight21.Humans
{
	/// <summary>
	/// This object will provide the required footer information for a specified protocolDocument consent form.
	/// </summary>
	public class ConsentFormFooter
	{
      #region column Enumeration
      private enum ConsentFooter
      {
         DocumentName,
         ProtocolNumber, 
         Version,
         ApprovalDate,
         ExpireDate,
         AmendmentNumber,
         AmendmentApprovalDate,
         SponsorAmendmentNumber	
      }
      #endregion

		#region Instance variables (private)
      private int protocolDocumentId;
      private int documentId;
		private string consentFormName;
		private string irbProtocolNumber;
		private string sponsorProtocolNumber;
		private string consentFormApprovalDate;
		private string irbExpirationDate;
		private string irbAmendmentNumber;
		private string irbAmendmentApprovalDate;
		private string sponsorApprovalNumber;
		#endregion
	
		#region constructors
      /// <summary>
      /// Defailt constructor
      /// </summary>
		public ConsentFormFooter()
		{		
		}
		#endregion

		# region public properties
      /// <summary>
      /// Protocol Document Id
      /// </summary>
      public int ProtocolDocumentId
      {
         get{return protocolDocumentId;}
      }

      /// <summary>
      /// Document Id
      /// </summary>
      public int DocumentId
      {
         get{return documentId;}
      }

		/// <summary>
		/// Name of the consent form attachment(e.g. Subject Population)
		/// </summary>
		public string ConsentFormName
		{
			get{return consentFormName;}
		}

		/// <summary>
		/// Protocol number from the initial review board
		/// </summary>
		public string IrbProtocolNumber
		{
			get{return irbProtocolNumber;}
		}

		/// <summary>
		/// Protocol Version(As Defined by the sponsor)
		/// </summary>
		public string SponsorProtocolNumber
		{
			get{return sponsorProtocolNumber;}
		}

		/// <summary>
		/// Protocol approval date set by the Rex feed once approved by the IRB.
		/// </summary>
		public string ConsentFormApprovalDate
		{
			get{return consentFormApprovalDate;}
		}

		/// <summary>
		/// Protocol expiration date
		/// </summary>
		public string IrbExpirationDate
		{
			get{return irbExpirationDate;}
		}

		/// <summary>
		/// IRB amendment number:
		/// </summary>
		public string IrbAmendmentNumber
		{
			get{return irbAmendmentNumber;}
		}

		/// <summary>
		/// IRB amendment approval date
		/// </summary>
		public string IrbAmendmentApprovalDate
		{
			get{return irbAmendmentApprovalDate;}
		}

		/// <summary>
		/// Sponsor Amendment Number
		/// </summary>
		public string SponsorApprovalNumber
		{
			get{return sponsorApprovalNumber;}
		}

		#endregion

		/// <summary>
		/// Load up the Consent form footer details based on document Id
		/// </summary>
		/// <param name="connectionString">DB connection string.</param>
		/// <param name="args">params collection (Document Id)</param>
		public void Load(string connectionString, params object[] args)
		{
			if (args.Length != 1)
			{
				throw new Exception("ConsentFormFooter.Load expects a ProtocolDocument.");
			}

			protocolDocumentId = ((ProtocolDocument)args[0]).Id;
			documentId = ((ProtocolDocument)args[0]).DocumentId;

			DataTable consentFooterDataTable =
			Utility.DataAccess.GetDataTable(connectionString, "GetConsentFormFooter",
				Utility.DataAccess.BuildSqlParameterArray("@protocolDocumentId", SqlDbType.Int, protocolDocumentId));

			if (consentFooterDataTable.Rows.Count > 0)
			{
				const string notApplicable = "N/A";

				DataRow consentDetailrow = consentFooterDataTable.Rows[0];

				consentFormName = (string)consentDetailrow[(int)ConsentFooter.DocumentName];
				object tempObject = consentDetailrow[(int)ConsentFooter.ProtocolNumber];
				if (DBNull.Value == tempObject) 
				{
					irbProtocolNumber = notApplicable;
				}
				else
				{
					irbProtocolNumber = (string)tempObject;
					if (string.Empty == irbProtocolNumber)
						irbProtocolNumber = notApplicable;
				}

                tempObject = consentDetailrow[(int)ConsentFooter.Version];
                sponsorProtocolNumber =
                   (DBNull.Value == tempObject) ? notApplicable : tempObject.ToString();

				tempObject = consentDetailrow[(int)ConsentFooter.ApprovalDate];
				consentFormApprovalDate =
				   (DBNull.Value == tempObject) ? notApplicable : ((DateTime)tempObject).ToShortDateString();

				tempObject = consentDetailrow[(int)ConsentFooter.ExpireDate];
				irbExpirationDate =
				   (DBNull.Value == tempObject) ? notApplicable : ((DateTime)tempObject).ToShortDateString();

				tempObject = consentDetailrow[(int)ConsentFooter.AmendmentNumber];
				if (DBNull.Value == tempObject)
				{
					irbAmendmentNumber = notApplicable;
				}
				else
				{
					irbAmendmentNumber = (string)tempObject;
					if (string.Empty == irbAmendmentNumber)
					{
						irbAmendmentNumber = notApplicable;
					}
				}

				tempObject = consentDetailrow[(int)ConsentFooter.AmendmentApprovalDate];
				irbAmendmentApprovalDate =
			   (DBNull.Value == tempObject) ? notApplicable : ((DateTime)tempObject).ToShortDateString();

				sponsorApprovalNumber = consentDetailrow[(int)ConsentFooter.SponsorAmendmentNumber].ToString();
				if (string.Empty == sponsorApprovalNumber)
				{
					sponsorApprovalNumber = notApplicable;
				}
			}
		}
	}
}
